package com.zyd.shiro.business.controller.pak;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Borrow;
import com.zyd.shiro.business.entity.Distribute;
import com.zyd.shiro.business.service.PakAssetsService;
import com.zyd.shiro.business.service.PakBorrowService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.PakDistribute;
import com.zyd.shiro.util.ResultUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zyd.shiro.persistence.beans.PakBorrow;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.io.File;
import com.zyd.shiro.utils.FileUtils;
import com.zyd.shiro.utils.ExcelUtil;
import java.util.Date;


@RestController
@RequestMapping("/borrow")
@Api(description = "资产借用")
public class PakBorrowController {

    @Autowired
    private PakBorrowService borrowService;

    @Autowired
    private PakAssetsService assetsService;

    @PostMapping
    @ApiOperation(value="添加", produces="application/json")
    public ResponseVO insert(Borrow borrow) {
        return ResultUtil.success("添加成功！",borrowService.insert(borrow));
    }

    @PutMapping
    @ApiOperation(value="修改", produces="application/json")
    public ResponseVO update(Borrow borrow) {
        borrowService.updateSelective(borrow);
        return ResultUtil.success("修改成功！",null);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value="主键删除", produces="application/json")
    public ResponseVO delete(@PathVariable(name="id") Long id){
        borrowService.removeByPrimaryKey(id);
        return ResultUtil.success("删除成功！",null);
    }

    @GetMapping
    @ApiOperation(value="分页查询", produces="application/json")
    public PageResult page(BaseConditionVO vo) {
        PageInfo<Borrow> pageInfo = borrowService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @GetMapping(value = "/all")
    @ApiOperation(value="查询所有", produces="application/json")
    public ResponseVO list() {
        List<Borrow> borrows = borrowService.listAll();
        return ResultUtil.success("查询成功！",borrows);
    }

 /*   @PostMapping(value = "/import")
    @ApiOperation(value="批量导入", produces="application/json")
    public ResponseVO folderImport(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.indexOf("."), fileName.length());
        if (suffix.equals(".xlsx") || suffix.equals(".xls")) {
            File newFile = new File(FileUtils.createFilePath(fileName));
            file.transferTo(newFile);
            ExcelUtil<PakBorrow> excelUtil = new ExcelUtil<>();
            List<PakBorrow> acfForms = excelUtil.importExcel("Sheet1", newFile, PakBorrow.class);
            for (PakBorrow a : acfForms) {
                a.setCreateTime(new Date());
                a.setUpdateTime(new Date());
                borrowService.increase(a);
            }
            return ResultUtil.success("修改成功！");
        }
        return ResultUtil.error("请输入excel文件!");
    }*/

    @PutMapping(value = "/borrow/{id}")
    @ApiOperation(value="借用", produces="application/json")
    public ResponseVO receive(@PathVariable(name="id") Long id) {
        Borrow borrow = borrowService.getByPrimaryKey(id);
        PakBorrow pakBorrow = borrow.getPakBorrow();
        if(pakBorrow.getAppstatus()!=1){
            return ResultUtil.error("未通过审批！");
        }
        if(pakBorrow.getBostatus()!=0){
            return ResultUtil.error("您已借用！");
        }

        assetsService.brrow(pakBorrow);
        pakBorrow.setBostatus(1);
        borrowService.updateSelective(borrow);
        return ResultUtil.success("借用成功！",borrow);
    }
    @PutMapping(value = "/approve/{id}")
    @ApiOperation(value="审批", produces="application/json")
    public ResponseVO approve(@PathVariable(name="id") Long id,int status) {
        Borrow borrow = borrowService.getByPrimaryKey(id);
        borrow.getPakBorrow().setAppstatus(status);
        borrowService.updateSelective(borrow);
        return ResultUtil.success("审批成功！",null);
    }
}